package com.example.win.android_banduyujia.lib.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Win on 2015/8/18.
 */
public class FileUtils {
   public static final String  LOG_TAG = FileUtils.class.getSimpleName();

    public static boolean exists(String path) {
        File file = new File(path);
        return file.exists();
    }

    public static boolean fileCpy(String srcPath, String dstPath, boolean rewrite) {
        File  srcFile = null;
        File  dstFile = null;
        FileInputStream   fisfrom = null;
        FileOutputStream  fosfrom = null;
        try{
            srcFile = new File(srcPath);
            dstFile = new File(dstPath);
            if(!srcFile.exists()){
                return false;
            }
            if(!srcFile.isFile()){
                return false;
            }
            if(!srcFile.canRead()){
                return false;
            }
            if(!dstFile.getParentFile().exists()){
                dstFile.getParentFile().mkdirs();
            }
            if(dstFile.exists() && rewrite){
                dstFile.delete();
            }
            fisfrom = new FileInputStream(srcFile);
            fosfrom = new FileOutputStream(dstFile, false);
            byte  b[] = new byte[1024];
            int c;
            while ((c = fisfrom.read(b)) > 0){
                fosfrom.write(b, 0, c);
            }
            return  true;
        }catch (Exception e){

            return false;
        }finally {
            if(fisfrom !=null){
                try{
                   fisfrom.close();
                }catch (IOException e){

                }
            }
            if(fosfrom !=null){
               try{
                   fosfrom.close();
               }catch (IOException e){

               }
            }
        }
    }

    public static void clearFileInDir(String path) {
          if(StrUtils.isEmpty(path)){
              return;
          }
        File  file = new File(path);
        if(file.exists() && file.isDirectory()){
            File[]  files = file.listFiles();
            if(files !=null && files.length > 0){
                for(File file1: files){
                    file1.delete();
                }
            }
        }
    }

    /**
     * 读取文本数据
     * @param context
     * @param fileName
     * @return
     */
    public static String readAssets(Context context, String fileName) {
        InputStream  is = null;
        String   content = null;
        try{
            is = context.getAssets().open(fileName);
            if(is !=null){
                byte[]  buffer = new byte[1024];
                ByteArrayOutputStream  bos = new ByteArrayOutputStream();
                while (true){
                    int readLength = is.read(buffer);
                    if(readLength == -1)
                        break;
                   bos.write(buffer, 0, readLength);
                }
                is.close();
                bos.close();
                content = new String(bos.toByteArray());
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();

        }catch (IOException e){
            e.printStackTrace();
            content = null;
        }finally {
            try{
                if(is !=null)
                    is.close();
            }catch (IOException e1){
                 e1.printStackTrace();
            }
        }
        return content;
    }

    public static String SDPATH = Environment.getExternalStorageDirectory() + "/formats/";

    public static void saveBitmap(Bitmap bm, String picName) {
        Log.e("", "保存图片");
        try {
            if (!isFileExist("")) {
                File tempf = createSDDir("");
            }
            File f = new File(SDPATH, picName + ".JPEG");
            if (f.exists()) {
                f.delete();
            }
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            Log.e("", "已经保存");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static File createSDDir(String dirName) throws IOException{
        File dir = new File(SDPATH + dirName);
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

            System.out.println("createSDDir:" + dir.getAbsolutePath());
            System.out.println("createSDDir:" + dir.mkdir());
        }
        return dir;
    }

    public static boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);
        file.isFile();
        return file.exists();
    }

    public static void delFile(String fileName) {
        File file = new File(SDPATH + fileName);
        if (file.isFile())
        {
            file.delete();
        }
        file.exists();
    }

    public static void deleteDir() {
        File dir = new File(SDPATH);
        if (dir == null || !dir.exists() || !dir.isDirectory())
            return;

        for (File file : dir.listFiles()) {
            if (file.isFile())
                file.delete(); // 删除所有文件
            else if (file.isDirectory())
                deleteDir(); // 递规的方式删除文件夹
        }
        dir.delete();// 删除目录本身
    }

    public static boolean fileIsExists(String path) {
        try {
            File f = new File(path);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {

            return false;
        }
        return true;
    }
}
